package cn.edu.hqu.cst.lecture02_02.model;

import lombok.Data;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class Movie {
    private Long id;
    private static List<String> posterUrls = new ArrayList<>();

    @NotBlank(message = "标题不能为空")
    @Size(max = 100, message = "标题长度不能超过 100 个字符")
    private String title;

    @NotBlank(message = "导演不能为空")
    @Size(max = 100, message = "导演长度不能超过 100 个字符")
    private String director;

    @Size(max = 500, message = "描述长度不能超过 500 个字符")
    private String description;

    private String posterUrl;
	@Valid
	private Movie movie;
	@NotBlank(message="评论内容不能为空")
	@Size(max=500,message="评论长度不能超过500个字符")
	private String content;
	@NotNull(message="评分不能为空")
	@Min(value=1,message="评分至少为1")
	@Max(value=5,message="评分不能超过5")
	private Integer rating;
	private static List<Comment> comments=new ArrayList<>();
	public static List<Comment> getCommentsByMovieId(Long movieId){
		return comments.stream().filter(comment->comment.getMovie().getId().equals(movieId))
				.collect(Collectors.toList());
	}
    public static void addComment(Comment comment) {
        comment.setId((long) (comments.size() + 1)); // 简单的 ID 赋值逻辑
        comments.add(comment);
    }

    // 静态列表模拟数据库
    private static List<Movie> movies = new ArrayList<>();

    // 使用静态方法来模拟数据库操作
    public static List<Movie> getAllMovies() {
        return new ArrayList<>(movies);
    }

    public static void addMovie(Movie movie) {
        movie.setId((long) (movies.size() + 1)); // 简单的 ID 赋值逻辑
        movies.add(movie);
    }

    public static Movie getMovieById(Long id) {
        return movies.stream()
                .filter(movie -> movie.getId().equals(id))
                .findFirst()
                .orElse(null);
    }
    public static void addPosterUrl(String url) {
        posterUrls.add(url);
    }
    // 设置海报 URL 并同步到静态数组
    public void setPosterUrl(String posterUrl) {
        this.posterUrl = posterUrl;
        addPosterUrl(posterUrl);
    }
    // 初始化静态数据列表
    static {
        movies.add(new Movie(1L, "电影1", "导演1", "描述1", "/api/movies/posters/poster1.jpg"));
        movies.add(new Movie(2L, "电影2", "导演2", "描述2", "/api/movies/posters/poster2.jpg"));
        posterUrls.add("/api/movies/posters/poster1.jpg");
        posterUrls.add("/api/movies/posters/poster2.jpg");
    }

    // 构造函数
    public Movie(Long id, String title, String director, String description, String posterUrl) {
        this.id = id;
        this.title = title;
        this.director = director;
        this.description = description;
        this.posterUrl = posterUrl;
    }
}